﻿namespace WMS6.Web.Common
{
    using System;
    using System.Data;
    using System.Text;
    using System.Web.UI.HtmlControls;
    using WMS6.Web;

    public class MenuAjax : BasePage
    {
        protected HtmlForm form1;
        protected HtmlHead Head1;

        private void AjaxResponse(string StrHtml)
        {
            base.Response.Clear();
            base.Response.StatusCode = 200;
            base.Response.StatusDescription = "OK";
            base.Response.ContentType = "text/html";
            base.Response.Write(StrHtml);
            base.Response.Flush();
            base.Response.End();
        }

        private void GetMenuData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["WhName"]))
            {
                base.UserInfo.CurrentWH = base.Request.QueryString["WhName"];
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["isGlobal"]))
            {
                base.UserInfo.IsGlobal = base.Request.QueryString["isGlobal"];
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["WhDbDesc"]))
            {
                base.UserInfo.WhDbDesc = base.Request.QueryString["WhDbDesc"];
            }
            string isShowQuery = base.GetValueFromAppSettings("IS_SHOW_QUERY");
            if (string.IsNullOrEmpty(isShowQuery))
            {
                isShowQuery = "Y";
            }
            DataSet menuQueryEntity = RoleFunctionServiceClientProxy.GetInstance().GetRoleMenuData(base.UserInfo.UserID, base.UserInfo.IsGlobal, isShowQuery, base.UserInfo.Preference.Lang);
            if ((menuQueryEntity != null) && (menuQueryEntity.Tables.Count > 0))
            {
                string strWidth = string.Empty;
                if (base.UserInfo.Preference.Lang.ToUpper() == "ZH-CN")
                {
                    strWidth = "160px";
                }
                else
                {
                    strWidth = "250px";
                }
                StringBuilder StrHtml = new StringBuilder();
                StrHtml.AppendFormat("<table  width=\"{0}\" cellpadding=\"0\" cellspacing=\"0\" border=0  style=\"border-width:0;\">", strWidth);
                for (int i = 0; menuQueryEntity.Tables[0].Rows.Count > i; i++)
                {
                    if ((menuQueryEntity.Tables[0].Rows[i]["PARENT_GROUP_GID"].ToString() == "-1") && (menuQueryEntity.Tables[1].Rows.Count > 0))
                    {
                        int q = 0;
                        for (int m = 0; menuQueryEntity.Tables[1].Rows.Count > m; m++)
                        {
                            if (((menuQueryEntity.Tables[0].Rows[i]["GROUP_ID"].ToString() == menuQueryEntity.Tables[1].Rows[m]["A"].ToString()) && (!string.IsNullOrEmpty(menuQueryEntity.Tables[1].Rows[m]["C"].ToString()) && !string.IsNullOrEmpty(menuQueryEntity.Tables[1].Rows[m]["D"].ToString()))) && (q == 0))
                            {
                                StrHtml.AppendFormat("<tr id=\"{0}{1}_tr\" ><td  width=\"10px\"  onFocus=\"this.blur()\" onmousemove=\"MenuMousemove('{0}{1}');\" onmouseout =\"MenuMouseout('{0}{1}');\" onclick=\"MenuClick('{0}{1}','1');\" ><a id=\"{0}{1}_Aclick\"  ><div  id=\"{0}{1}_img\"  class=\"add\" /></a></td>", menuQueryEntity.Tables[0].Rows[i]["GROUP_ID"].ToString(), menuQueryEntity.Tables[0].Rows[i]["TEXT"].ToString());
                                StrHtml.AppendFormat(" <td id=\"{0}{1}_td\" style=\"white-space:nowrap;\" onmousemove=\"MenuMousemove('{0}{1}');\"onmouseout =\"MenuMouseout('{0}{1}');\" onclick=\"MenuClick('{0}{1}');\" ><a  href=\"#\"  onFocus=\"this.blur()\"    id=\"{0}{1}_A\">{1}</a></td></tr> ", menuQueryEntity.Tables[0].Rows[i]["GROUP_ID"].ToString(), menuQueryEntity.Tables[0].Rows[i]["TEXT"].ToString());
                                StrHtml.AppendFormat("<tr  id=\"{0}{1}_tr\"><td ></td> <td align=left><div id=\"{0}{1}_div\"  style=\"display:none;\">", menuQueryEntity.Tables[0].Rows[i]["GROUP_ID"].ToString(), menuQueryEntity.Tables[0].Rows[i]["TEXT"].ToString());
                                for (int j = 0; menuQueryEntity.Tables[0].Rows.Count > j; j++)
                                {
                                    if (menuQueryEntity.Tables[0].Rows[i]["GROUP_ID"].ToString() == menuQueryEntity.Tables[0].Rows[j]["PARENT_GROUP_GID"].ToString())
                                    {
                                        int p = 0;
                                        for (int z = 0; menuQueryEntity.Tables[1].Rows.Count > z; z++)
                                        {
                                            if (((menuQueryEntity.Tables[0].Rows[j]["GROUP_ID"].ToString() == menuQueryEntity.Tables[1].Rows[z]["B"].ToString()) || (menuQueryEntity.Tables[0].Rows[j]["GROUP_ID"].ToString() == menuQueryEntity.Tables[1].Rows[z]["C"].ToString())) && (p == 0))
                                            {
                                                StrHtml.AppendFormat(" <table width=\"100%\"  border=0 cellpadding=\"0\" cellspacing=\"0\"  style=\"border-width:0;\"><tr  id=\"{0}{1}_tr\">", menuQueryEntity.Tables[0].Rows[j]["GROUP_ID"].ToString(), menuQueryEntity.Tables[0].Rows[j]["TEXT"].ToString());
                                                if (!string.IsNullOrEmpty(menuQueryEntity.Tables[0].Rows[j]["URL"].ToString()))
                                                {
                                                    StrHtml.AppendFormat(" <td></td><td style=\"white-space:nowrap;\" onFocus=\"this.blur()\" onclick=\"ClassClick('{0}{1}');\"  onmousemove=\"MenuMousemove('{0}{1}');\"onmouseout =\"MenuMouseout('{0}{1}');\" ><a href=\"{2}?FunctionID={3}\"    id=\"{0}{1}_A\" target=\"main\">&nbsp;{1}</a></td></tr> ", new object[] { menuQueryEntity.Tables[0].Rows[j]["GROUP_ID"].ToString(), menuQueryEntity.Tables[0].Rows[j]["TEXT"].ToString(), menuQueryEntity.Tables[0].Rows[j]["URL"].ToString().Replace("~/", ""), menuQueryEntity.Tables[0].Rows[j]["FUNCTION_ID"].ToString() });
                                                }
                                                else
                                                {
                                                    StrHtml.AppendFormat(" <td  id=\"{0}{1}_td\"  width=\"10px\" onFocus=\"this.blur()\"  onmousemove=\"MenuMousemove('{0}{1}');\" onmouseout =\"MenuMouseout('{0}{1}');\"  ><a id=\"{0}{1}_Aclick\"   onclick=\"MenuClick('{0}{1}','2');\" ><div  id=\"{0}{1}_img\"   class=\"add\" /></a></td><td style=\"white-space:nowrap;\" onclick=\"MenuClick('{0}{1}');\" ><a  href=\"#\"   id=\"{0}{1}\">{1}</a></td></tr> ", menuQueryEntity.Tables[0].Rows[j]["GROUP_ID"].ToString(), menuQueryEntity.Tables[0].Rows[j]["TEXT"].ToString(), menuQueryEntity.Tables[0].Rows[j]["URL"].ToString().Replace("~/", ""));
                                                    StrHtml.AppendFormat("<tr><td></td> <td align=\"left\"><div  id=\"{0}{1}_div\" style=\"display:none;\">", menuQueryEntity.Tables[0].Rows[j]["GROUP_ID"].ToString(), menuQueryEntity.Tables[0].Rows[j]["TEXT"].ToString(), menuQueryEntity.Tables[0].Rows[j]["URL"].ToString().Replace("~/", ""));
                                                    StrHtml.AppendFormat("{0}", this.RecursiveMenu(menuQueryEntity.Tables[0].Rows[j]["GROUP_ID"].ToString(), menuQueryEntity));
                                                    StrHtml.AppendLine("</div></td></tr>");
                                                }
                                                StrHtml.AppendLine(" </table>");
                                                p++;
                                            }
                                        }
                                    }
                                }
                                StrHtml.AppendLine("</div></td></tr>");
                                q++;
                            }
                        }
                    }
                }
                StrHtml.AppendLine(" </table>");
                this.AjaxResponse(StrHtml.ToString());
            }
            else
            {
                this.AjaxResponse("");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Page.Response.Buffer = false;
            this.Page.Response.Cache.SetNoStore();
            if (!string.IsNullOrEmpty(base.Request.QueryString["TYPE"]))
            {
                this.GetMenuData();
            }
        }

        private string RecursiveMenu(string GroupID, DataSet menuQueryEntity)
        {
            StringBuilder StrHtml = new StringBuilder();
            for (int i = 0; menuQueryEntity.Tables[0].Rows.Count > i; i++)
            {
                if (menuQueryEntity.Tables[0].Rows[i]["PARENT_GROUP_GID"].ToString() == GroupID)
                {
                    for (int j = 0; menuQueryEntity.Tables[1].Rows.Count > j; j++)
                    {
                        if (((GroupID == menuQueryEntity.Tables[1].Rows[j]["B"].ToString()) && (menuQueryEntity.Tables[0].Rows[i]["GROUP_ID"].ToString() == menuQueryEntity.Tables[1].Rows[j]["C"].ToString())) || ((GroupID == menuQueryEntity.Tables[1].Rows[j]["C"].ToString()) && (menuQueryEntity.Tables[0].Rows[i]["GROUP_ID"].ToString() == menuQueryEntity.Tables[1].Rows[j]["D"].ToString())))
                        {
                            StrHtml.AppendLine("<table width=\"100%\"  border=0 cellpadding=\"0\" cellspacing=\"0\"  style=\"border-width:0;\">");
                            StrHtml.AppendFormat(" <tr  id=\"{0}{1}_tr\"><td  width=\"10px\"></td><td><a id=\"{0}{1}_Aclick\" href=\"{2}?FunctionID={3}\"  onFocus=\"this.blur()\"  target=\"main\"  onclick=\"ClassClick('{0}{1}');\" onmousemove=\"MenuMousemove('{0}{1}');\"onmouseout =\"MenuMouseout('{0}{1}');\" >{1}</a></td></tr>", new object[] { menuQueryEntity.Tables[0].Rows[i]["GROUP_ID"].ToString(), menuQueryEntity.Tables[0].Rows[i]["TEXT"].ToString(), menuQueryEntity.Tables[0].Rows[i]["URL"].ToString().Replace("~/", ""), menuQueryEntity.Tables[0].Rows[i]["FUNCTION_ID"].ToString() });
                            StrHtml.AppendLine("</table>");
                            break;
                        }
                    }
                }
            }
            return StrHtml.ToString();
        }
    }
}

